<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_management extends AppModel {
    var $_primary_key = ADMIN_ADMIN_ID;
    var $_tablename = ADMIN;
    var $_mapping = array(
    array('id' => ADMIN_ADMIN_ID),
    array('name' => ADMIN_NAME),
    array('login_name' => ADMIN_LOGIN_NAME),
    array('admin_type' => ADMIN_TYPE),
    array('start_date' => ADMIN_START_DATE),
    array('admin_password' => ADMIN_ADMIN_PASSWORD),
    array('default' => ADMIN_DEFAULT_FLAG),
    );
    public function User_management() {
        parent::AppModel();
    }

    function save($admin_id = NULL, $f_data) {
        $CI = &get_instance();
        if(is_null($admin_id)) {
            $this->db->set($f_data);
            $this->db->insert($this->_tablename);
            return $this->db->insert_id();
        }else {
            $this->db->set($f_data);
            $this->db->where($this->_primary_key, $admin_id);
            $this->db->update($this->_tablename);
            return $admin_id;
        }
    }

    function check_login_name($admin_id=NULL, $f_name) {
        $conditions = array(ADMIN_NAME=>$f_name);
        if(!is_null($admin_id))
            $this->db->where($this->_primary_key.' <> ', $admin_id);
        return $this->db->get_where($this->_tablename, $conditions)->num_rows();
    }

    function get_last_id() {
        $this->db->select('MAX('.$this->_primary_key.') AS last_id');
        $result = $this->db->get($this->_tablename)->row();
        if(empty($result->last_id))
            return 1;
        else
            return $result->last_id + 1;
    }

    function delete() {
        $arr = array(ADMIN);
        foreach($arr as $table) {
            $this->db->where('admin_id', $this->get_id());
            $this->db->delete($table);
        //
        }
    }
	/*
    function get_default_fid() {
        $this->db->select(ADMIN_ADMIN_ID);
        $this->db->from(ADMIN);
        $this->db->where(ADMIN_DEFAULT_FLAG, '1');
        $this->db->limit(1);
        $this->db->order_by(ADMIN_ADMIN_ID, 'ASC');
        $res = $this->db->get()->row_array();
        return $res[ADMIN_ADMIN_ID];
    }
	*/
    function has_data($fid) {
        $arr = array(BLOCK, CELL_LINK, COURSE, COURSE_INSTANCE, COURSE_STAGE, IMAGE, LINK, LOCATION, NAVIGATION, PAGE, PARAMETER, PAYMENT_OPTION, PRODUCT, STAGE, TEAM, PHOTOSET, TERM, PRICE_LIST, TESTIMONIAL, TREASURE_BAG, WAITING_LIST, ENROLMENT, PAGE_CSS, TEMPLATES, GROUP, PRICE_GROUP);
        foreach($arr as $table) {
            $this->db->where('admin_id', $fid);
            if($this->db->count_all_results($table) > 0) {
                return TRUE;
            }
        }
        return FALSE;
    }
	/*
    function migrate($to_fid = NULL) {
        $from_fid = $this->get_default_fid();

        if(empty($to_fid)) {
            $to_fid = $this->get_last_id();

            $sql = 'INSERT INTO '.ADMIN.' SELECT '.$to_fid.', '.ADMIN_NAME.', '.ADMIN_TYPE.', '.ADMIN_TITLE.', '.ADMIN_ADMINE.', '.ADMIN_ADDRESS_ID.', '.ADMIN_START_DATE.', '.ADMIN_END_DATE.', \''.md5(ADMIN_ADMINE).'\', 0 FROM '.ADMIN.' WHERE '.ADMIN_ADMIN_ID.' = '.$from_fid;
            $this->db->query($sql);
        //echo $this->db->last_query();
        }
        $sql = 'INSERT INTO '.ADDRESS.'('.ADDRESS_ADMIN_ID.', '.ADDRESS_LINE1.', '.ADDRESS_LINE2.', '.ADDRESS_LINE3.', '.ADDRESS_CITY.', '.ADDRESS_POSTCODE.', '.ADDRESS_STATE.', '.ADDRESS_PHONE.', '.ADDRESS_MOBILE.', '.ADDRESS_FAX.', '.ADDRESS_EMAIL.')
                    SELECT '.$to_fid.', '.ADDRESS_LINE1.', '.ADDRESS_LINE2.', '.ADDRESS_LINE3.', '.ADDRESS_CITY.', '.ADDRESS_POSTCODE.', '.ADDRESS_STATE.', '.ADDRESS_PHONE.', '.ADDRESS_MOBILE.', '.ADDRESS_FAX.', '.ADDRESS_EMAIL.' 
                    FROM '.ADDRESS.' WHERE '.ADDRESS_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);

        $sql = 'INSERT INTO '.BLOCK.'
                SELECT '.$to_fid.', '.BLOCK_BLOCK_ID.', '.BLOCK_BLOCK_TYPE.', '.BLOCK_NAME.', '.BLOCK_FUNCTION_NAME.', '.BLOCK_CONTENT.', '.BLOCK_OBJ_ID.', '.BLOCK_SHOW_FLAG.' 
                FROM '.BLOCK.' WHERE '.BLOCK_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.CELL_LINK.'
                SELECT '.$to_fid.', '.CELL_LINK_PAGE_ID.', '.CELL_LINK_COL_ID.', '.CELL_LINK_BLOCK_ID.', '.CELL_LINK_ORDER_ID.' 
                FROM '.CELL_LINK.' WHERE '.CELL_LINK_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.COURSE.'
                SELECT '.$to_fid.', '.COURSE_PRODUCT_ID.', '.COURSE_CLASS_DURATION.', '.COURSE_NOTES.', '.COURSE_IMAGEJSON.', '.COURSE_IMAGEPATH.', '.COURSE_GROUP_ID.'
                FROM '.COURSE.' WHERE '.COURSE_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.COURSE_INSTANCE.'
                SELECT '.$to_fid.', '.COURSE_INSTANCE_PRODUCT_ID.', '.COURSE_INSTANCE_TERM_ID.', '.COURSE_INSTANCE_CLASS_ID.', '.COURSE_INSTANCE_LOCATION_ID.', '.COURSE_INSTANCE_TEACHER_ID.', '.COURSE_INSTANCE_ASSISTANT_ID.', '.COURSE_INSTANCE_START_DATE.', '.COURSE_INSTANCE_END_DATE.', '.COURSE_INSTANCE_DAY.', '.COURSE_INSTANCE_START_TIME.', '.COURSE_INSTANCE_END_TIME.', '.COURSE_INSTANCE_STATUS.'
                FROM '.COURSE_INSTANCE.' WHERE '.COURSE_INSTANCE_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO `'.GROUP.'`
                SELECT '.$to_fid.', '.GROUP_GROUP_ID.', '.GROUP_NAME.'
                FROM `'.GROUP.'` WHERE '.GROUP_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.PRICE_GROUP.'
                SELECT '.$to_fid.', '.PRICE_GROUP_PRICE_ID.', '.PRICE_GROUP_GROUP_ID.', '.PRICE_GROUP_PRICE.'
                FROM '.PRICE_GROUP.' WHERE '.PRICE_GROUP_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.COURSE_STAGE.'
                SELECT '.$to_fid.', '.COURSE_STAGE_COURSE_ID.', '.COURSE_STAGE_STAGE_ID.' 
                FROM '.COURSE_STAGE.' WHERE '.COURSE_STAGE_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);

        $sql = 'INSERT INTO '.LINK.'
                SELECT '.$to_fid.', '.LINK_LINK_ID.', '.LINK_NAME.', '.LINK_URL.', '.LINK_DESCRIPTION.', '.LINK_IMAGE_ID.', '.LINK_LINK_PAGE.', '.LINK_IMAGEJSON.', '.LINK_IMAGEPATH.'  
                FROM '.LINK.' WHERE '.LINK_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.LOCATION.'
                SELECT '.$to_fid.', '.LOCATION_LOCATION_ID.', '.LOCATION_TYPE.', '.LOCATION_ADDRESS_ID.', '.LOCATION_NAME.', '.LOCATION_MAP.', '.LOCATION_DIRECTIONS1.', '.LOCATION_DIRECTIONS2.', '.LOCATION_IMAGE_ID.', '.LINK_IMAGEJSON.', '.LINK_IMAGEPATH.' 
                FROM '.LOCATION.' WHERE '.LOCATION_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.NAVIGATION.'
                SELECT '.$to_fid.', '.NAVIGATION_MENU_ID.', `'.NAVIGATION_TYPE.'`, `'.NAVIGATION_NAME.'`, '.NAVIGATION_TOPIC.', '.NAVIGATION_URL.', '.NAVIGATION_SUB_MENU_ID.', '.NAVIGATION_PAR_MENU_ID.', '.NAVIGATION_LANG_ID.', '.NAVIGATION_PID.', '.NAVIGATION_ITEM_VISIBLE.'  
                FROM '.NAVIGATION.' WHERE '.NAVIGATION_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.PAGE.'
                SELECT '.$to_fid.', '.PAGE_PAGE_ID.', '.PAGE_TITLE.', '.PAGE_CSS_FILE.', '.PAGE_IFRAME.', '.PAGE_VISIBLE.', '.PAGE_ACTIVE.', '.PAGE_CONTENT_MIDDLE.', '.PAGE_META_TITLE.', '.PAGE_META_DESCRIPTION.', '.PAGE_META_KEYWORD.', '.PAGE_CONTENT_LEFT.', '.PAGE_CONTENT_RIGHT.', '.PAGE_SHOW_FLAG.' 
                FROM '.PAGE.' WHERE '.PAGE_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();
      
        $sql = 'INSERT INTO '.PARAMETER.'
                SELECT '.$to_fid.', `'.PARAMETER_PARAM_ID.'`, `'.PARAMETER_NAME.'`, `'.PARAMETER_TYPE.'`, `'.PARAMETER_VALUE.'`, `'.PARAMETER_DESCRIPTION.'`, `'.PARAMETER_USER_EDIT.'`, `'.PARAMETER_OPTION.'` 
                FROM '.PARAMETER.' WHERE '.PARAMETER_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.PAYMENT_OPTION.'
                SELECT '.$to_fid.', '.PAYMENT_OPTION_CASH_OPT.', '.PAYMENT_OPTION_CASH_TXT.', '.PAYMENT_OPTION_CHEQUE_OPT.', '.PAYMENT_OPTION_CHEQUE_TXT.', '.PAYMENT_OPTION_BANK_OPT.', '.PAYMENT_OPTION_BANK_TXT.', '.PAYMENT_OPTION_CC_OPT.', '.PAYMENT_OPTION_CC_TXT.', '.PAYMENT_OPTION_PAYMENT_OK_TXT.', '.PAYMENT_OPTION_PAYPAL_OPT.', '.PAYMENT_OPTION_PAYPAL_ACC_ID.', '.PAYMENT_OPTION_CUST_ACC_ID.', '.PAYMENT_OPTION_PAYPAL_TXT.'  
                FROM '.PAYMENT_OPTION.' WHERE '.PAYMENT_OPTION_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();
      
        $sql = 'INSERT INTO '.PRODUCT.'
                SELECT '.$to_fid.', '.PRODUCT_PRODUCT_ID.', '.PRODUCT_TYPE.', '.PRODUCT_SUB_TYPE.', '.PRODUCT_PRICE.', '.PRODUCT_NAME.', '.PRODUCT_DESCRIPTION.', '.PRODUCT_IMAGE_ID.', '.PRODUCT_NOTE.', '.PRODUCT_ACTIVE.' 
                FROM '.PRODUCT.' WHERE '.PRODUCT_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.STAGE.'
                SELECT '.$to_fid.', '.STAGE_STAGE_ID.', '.STAGE_NAME.', '.STAGE_DESCRIPTION.', '.STAGE_IMAGE_ID.', '.STAGE_ORDER_POSITION.', '.LINK_IMAGEJSON.', '.LINK_IMAGEPATH.' 
                FROM '.STAGE.' WHERE '.STAGE_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        $sql = 'INSERT INTO '.TEAM.'
                SELECT '.$to_fid.', '.TEAM_MEMBER_ID.', '.TEAM_NAME.', '.TEAM_DOB.', '.TEAM_SEX.', '.TEAM_IMAGE_ID.', '.TEAM_PROFILE.', '.TEAM_ROLE.', '.TEAM_ACTIVE.', '.LINK_IMAGEJSON.', '.LINK_IMAGEPATH.' 
                FROM '.TEAM.' WHERE '.TEAM_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        //echo $this->db->last_query();

        // Create new term
        $sql = 'INSERT INTO '.TERM.'
                SELECT '.$to_fid.', '.TERM_TERM_ID.', '.TERM_YEAR.', '.TERM_TERM.', '.TERM_THEME.', '.TERM_WEEKS.', '.TERM_CURRENT.', '.TERM_START_DATE.', '.TERM_END_DATE.', '.TERM_TITLE.', '.TERM_NOTES.', '.TERM_ENROL_DATE.' 
                FROM '.TERM.' WHERE '.TEAM_ADMIN_ID.' = '.$from_fid;
        //FROM '.TERM.' WHERE '.TEAM_ADMIN_ID.' = '.$from_fid.' AND '.TERM_CURRENT.' = \'YES\'';
        $this->db->query($sql);
        //echo $this->db->last_query();

        $CI = &get_instance();
      
        // Create new treasure_bag with the current term only
        $sql = 'INSERT INTO '.TREASURE_BAG.'
                SELECT '.$to_fid.', '.TREASURE_BAG_TERM_ID.', '.TREASURE_BAG_WEEK_NUM.', '.TREASURE_BAG_WEEK_START.', '.TREASURE_BAG_TH_TODDLER.', '.TREASURE_BAG_TH_BABY.', '.TREASURE_BAG_TB_TODDLER.', '.TREASURE_BAG_TB_BABY.', '.TREASURE_BAG_TODDLER_IMAGE.', '.TREASURE_BAG_BABY_IMAGE.' 
                FROM '.TREASURE_BAG.' WHERE '.TREASURE_BAG_ADMIN_ID.' = '.$from_fid;
        //FROM '.TREASURE_BAG.' WHERE '.TREASURE_BAG_ADMIN_ID.' = '.$from_fid.' AND '.TREASURE_BAG_TERM_ID.' = '.$currrent_term_id;
        $this->db->query($sql);
        //echo $this->db->last_query();

        // Create new email templates
        $sql = 'INSERT INTO '.TEMPLATES.'
                SELECT '.TEMPLATES_TEMPLATE_TYPE_ID.', '.TEMPLATES_SUBJECT.', '.TEMPLATES_BODY.', '.$to_fid.' 
                FROM '.TEMPLATES.' WHERE '.TEMPLATES_ADMIN_ID.' = '.$from_fid;
        $this->db->query($sql);
        return $to_fid;
    }

    function export() {

        $this->db->select(ADMIN_ADMINE . ' as name,' . ADMIN_TYPE . ' as type,' . ADMIN_TITLE . ' as title,' . PARAMETER_VALUE . ' as theme');
        $this->db->from(ADMIN);
        $this->db->join(PARAMETER, PARAMETER . '.' . PARAMETER_ADMIN_ID . '=' . ADMIN . '.' . ADMIN_ADMIN_ID);
        $this->db->where(PARAMETER . '.' . PARAMETER_NAME, PRESENTATION_TEMPLATES);
        $result = $this->db->get();
        //Write to file...

        $this->load->helper('file');
        $path = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'xml' . DIRECTORY_SEPARATOR;
        foreach ($result->result() as $data) {
            $string = <<<XML
<SiteTitle>$data->title</SiteTitle>
<SiteType>$data->type</SiteType>
<SiteTheme>$data->theme<SiteTheme>
XML;
            if (!write_file($path . $data->name . '.xml', $string)) {
                $error[] = error_get_last();
            }
        }

        return isset($error) ? $error : true;
    }
	*/
}
?>